package handler

import (
	"fmt"
	"gitee.com/lanshiren/miniprogram/app/log"
	"gitee.com/lanshiren/miniprogram/app/model"
	"gitee.com/lanshiren/miniprogram/app/service"
	"github.com/gin-gonic/gin"
)

// HandleUpdateContactRole ...
func HandleUpdateContactRole(c *gin.Context) {
	var param model.UpdateUserRoleParam
	if err := c.BindJSON(&param); err != nil {
		log.MainLogger.Error(fmt.Sprintf("bind client json to UpdateUserRoleParam err: %v", err))
	}
	if err := service.UpdateUserRole(param.ContactID, param.NewRole); err != nil {
		c.JSON(500, gin.H{
			"result": NewResultRet(err.Error(), 500, nil),
		})
		return
	}
	c.JSON(200, gin.H{
		"result": NewResultRet("ok", 200, nil),
	})
	return
}

// HandleSearchContact ...
func HandleSearchContact(c *gin.Context) {
	var param model.SearchOneUserParam
	if err := c.BindQuery(&param); err != nil {
		log.MainLogger.Error(fmt.Sprintf("bind client json to SearchUsersParam err: %v", err))
	}
	if param.SearchKey == "" && param.Role == 10 {
		var param_ model.SearchUsersParam
		param_.Page = "0"
		param_.PageSize = "200"
		param_.RoleId = param.Role
		total, contacts, err := service.SearchContacts(param_)
		if err != nil {
			log.MainLogger.Error(fmt.Sprintf("SearchContacts err: %v", err))
			c.JSON(200, gin.H{
				"result": NewResultRet(err.Error(), 200, map[string]interface{}{
					"results": "",
				}),
			})
			return
		}
		c.JSON(200, gin.H{
			"result": NewResultRet("", 200, map[string]interface{}{
				"total":   total,
				"results": contacts,
			}),
		})
		return
	}
	user, err := service.GetContactByPhoneOrNickName(param.SearchKey, param.Role)
	if err != nil {
		log.MainLogger.Error(fmt.Sprintf("SearchContact err: %v", err))
		c.JSON(200, gin.H{
			"result": NewResultRet(err.Error(), 200, map[string]interface{}{
				"results": "",
			}),
		})
		return
	}
	if user.Nickname == "" {
		c.JSON(200, gin.H{
			"result": NewResultRet("", 200, map[string]interface{}{
				"results": "",
			}),
		})
		return
	}
	c.JSON(200, gin.H{
		"result": NewResultRet("ok", 200, map[string]interface{}{
			"results": []model.UserVO{*user},
		}),
	})
}

// HandleSearchContacts ...
func HandleSearchContacts(c *gin.Context) {
	var param model.SearchUsersParam
	if err := c.BindQuery(&param); err != nil {
		log.MainLogger.Error(fmt.Sprintf("bind client json to SearchUsersParam err: %v", err))
	}
	total, contacts, err := service.SearchContacts(param)
	if err != nil {
		log.MainLogger.Error(fmt.Sprintf("SearchContacts err: %v", err))
		c.JSON(500, gin.H{
			"result": NewResultRet(err.Error(), 500, nil),
		})
		return
	}
	c.JSON(200, gin.H{
		"result": NewResultRet("", 200, map[string]interface{}{
			"total":   total,
			"results": contacts,
		}),
	})
}

// UpdateUserInfo ...
func UpdateUserInfo(c *gin.Context) {
	// token -> openid
	var param model.UpdateUserParam
	if err := c.BindJSON(&param); err != nil {
		log.MainLogger.Error(fmt.Sprintf("bind client json to SearchUsersParam err: %v", err))
	}
	token := c.Request.Header.Get("token")
	oid, err := service.GetOpenIdByToken(token)
	if err != nil {
		log.MainLogger.Error(fmt.Sprintf("SearchContacts err: %v", err))
		c.JSON(500, gin.H{
			"result": NewResultRet(err.Error(), 500, nil),
		})
		return
	}
	// update user
	if err := model.QueryAndUpdateUserNameAndPhone(oid, &model.UsersDO{Phone: param.Phone, Nickname: param.NickName, AvatarUrl: param.AvatarUrl}); err != nil {
		log.MainLogger.Error(fmt.Sprintf("SearchContacts err: %v", err))
		c.JSON(500, gin.H{
			"result": NewResultRet(err.Error(), 500, nil),
		})
		return
	}
	c.JSON(200, gin.H{
		"result": NewResultRet("ok", 200, nil),
	})
	return
}
